package com.ruoyi.wx.service.impl;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import com.ruoyi.info.domain.VinfoCar;
import com.ruoyi.info.service.IVinfoCarService;
import org.springframework.stereotype.Service;
import com.ruoyi.wx.mapper.VehiclesLogMapper;
import com.ruoyi.wx.domain.VehiclesLog;
import com.ruoyi.wx.service.IVehiclesLogService;
import javax.annotation.Resource;

/**
 * 车辆日志Service业务层处理
 *
 * @author oy
 * @date 2022-12-13
 */
@Service
public class VehiclesLogServiceImpl implements IVehiclesLogService
{
    @Resource
    private VehiclesLogMapper vehiclesLogMapper;

    /**
     * 查询车辆日志
     *
     * @param vehicleLogId 车辆日志主键
     * @return 车辆日志
     */
    @Override
    public VehiclesLog selectVehiclesLogByVehicleLogId(Long vehicleLogId)
    {
        return vehiclesLogMapper.selectVehiclesLogByVehicleLogId(vehicleLogId);
    }

    /**
     * 查询车辆日志列表
     *
     * @param vehiclesLog 车辆日志
     * @return 车辆日志
     */
    @Override
    public List<VehiclesLog> selectVehiclesLogList(VehiclesLog vehiclesLog)
    {
        return vehiclesLogMapper.selectVehiclesLogList(vehiclesLog);
    }

    /**
     * 新增车辆日志
     *
     * @param vehiclesLog 车辆日志
     * @return 结果
     */
    @Override
    public int insertVehiclesLog(VehiclesLog vehiclesLog)
    {
        return vehiclesLogMapper.insertVehiclesLog(vehiclesLog);
    }

    /**
     * 修改车辆日志
     *
     * @param vehiclesLog 车辆日志
     * @return 结果
     */
    @Override
    public int updateVehiclesLog(VehiclesLog vehiclesLog)
    {
        return vehiclesLogMapper.updateVehiclesLog(vehiclesLog);
    }

    /**
     * 批量删除车辆日志
     *
     * @param vehicleLogIds 需要删除的车辆日志主键
     * @return 结果
     */
    @Override
    public int deleteVehiclesLogByVehicleLogIds(Long[] vehicleLogIds)
    {
        return vehiclesLogMapper.deleteVehiclesLogByVehicleLogIds(vehicleLogIds);
    }

    /**
     * 删除车辆日志信息
     *
     * @param vehicleLogId 车辆日志主键
     * @return 结果
     */
    @Override
    public int deleteVehiclesLogByVehicleLogId(Long vehicleLogId)
    {
        return vehiclesLogMapper.deleteVehiclesLogByVehicleLogId(vehicleLogId);
    }

    @Resource
    IVinfoCarService carService;

    @Override
    public Double selectToCountWeight(String deviceNo,Date beginTime,Date endTime) throws ParseException {
        VinfoCar car = carService.selectVinfoCarByDeviceNo(deviceNo);
        double initWeight = car.getIniweight();
        List<VehiclesLog> vehiclesLogList = vehiclesLogMapper.selectToCountWeight(car.getDeviceNo(),beginTime,endTime);
        double[] weightArray = new double[vehiclesLogList.size()];
        double countWeight = 0;
        for (int i = 0; i < vehiclesLogList.size(); i++){
            VehiclesLog vehiclesLog = vehiclesLogList.get(i);
            String eventDesc = vehiclesLog.getEventDesc();
            String weightstr = vehiclesLog.getEventDesc().substring(eventDesc.indexOf("AD0")+4,eventDesc.indexOf("AD1")-1);
            double weight = Double.parseDouble(weightstr);
            weightArray[i] = weight;
        }
        for (int i = 0; i < weightArray.length-1; i++) {
            if (i > 0) {
                if (weightArray[i] - weightArray[i-1] > 50 && weightArray[i] - weightArray[i+1] > 50){
                    countWeight += weightArray[i]-initWeight;
                }
            }
        }
        return countWeight;
    }

    @Override
    public Double selectCountWeight(String carNo, Date beginTime, Date endTime) {
        return vehiclesLogMapper.selectCountWeight(carNo,beginTime,endTime);
    }

    @Override
    public Date selectEarliestDate() {
        return vehiclesLogMapper.selectEarliestDate();
    }

    @Override
    public List<VehiclesLog> selectByCarNoAndDateRange(Long deviceNo, String carNo, Date beginTime, Date endTime) {
        return vehiclesLogMapper.selectByCarNoAndDateRange(deviceNo,carNo, beginTime,endTime);
    }

    @Override
    public List<VinfoCar> selectCar() {
        return vehiclesLogMapper.selectCar();
    }

    @Override
    public void deleteVehiclesLogByVehicleLogCreateTime(Date date) {
        vehiclesLogMapper.deleteVehiclesLogByVehicleLogCreateTime(date);
    }
}
